package com.spica.spicaschool.service


import com.amap.api.location.AMapLocation
import com.amap.api.maps.model.LatLng
import com.amap.api.trace.TraceLocation
import java.util.ArrayList


/**
 * 运动轨迹绘制工具类
 */

/**
 * 使用坐标轨迹数组绘制轨迹
 */
fun parseTraceLocationList(list: List<AMapLocation>): List<TraceLocation> {
    val traceList = mutableListOf<TraceLocation>()
    if (list.isEmpty()) {
        return traceList
    }

    list.forEach {
        val location = TraceLocation()
        location.bearing = it.bearing
        location.latitude = it.latitude
        location.longitude = it.longitude
        location.speed = it.speed
        location.time = it.time
        traceList.add(location)
    }

    return traceList
}


/**
 * 描述: 计算卡路里
 * ---------计算公式：体重（kg）* 距离（km）* 运动系数（k）
 * ---------运动系数：健走：k=0.8214；跑步：k=1.036；自行车：k=0.6142；轮滑、溜冰：k=0.518室外滑雪：k=0.888
 * 作者: james
 * 日期: 2019/2/20 19:40
 * 类名: MotionUtils
 *
 * @param weight   体重
 * @param distance 距离
 */
fun calculationCalorie(weight: Double, distance: Double): Double {
    return weight * distance * 1.036
}

fun amapLocationToString(location: AMapLocation): String {
    val locString = StringBuffer()
    locString.append(location.latitude).append(",")
    locString.append(location.longitude).append(",")
    locString.append(location.provider).append(",")
    locString.append(location.time).append(",")
    locString.append(location.speed).append(",")
    locString.append(location.bearing)
    return locString.toString()
}

fun amapLocationToString(location: LatLng): String {
    val locString = StringBuffer()
    locString.append(location.latitude).append(",")
    locString.append(location.longitude)
    return locString.toString()
}


fun parseLatLngLocation(latLonStr: String?): LatLng? {
    if (latLonStr == null || latLonStr == "" || latLonStr == "[]") {
        return null
    }
    val loc = latLonStr.split(",".toRegex()).toTypedArray()
    var location: LatLng? = null
    if (loc.size == 2) {
        location = LatLng(loc[0].toDouble(), loc[1].toDouble())
    }
    return location
}

fun parseLatLngLocations(latLonStr: String): ArrayList<LatLng>? {
    val locations = ArrayList<LatLng>()
    val latLonStrs = latLonStr.split(";".toRegex()).toTypedArray()
    for (latLonStr1 in latLonStrs) {
        val location: LatLng? = parseLatLngLocation(latLonStr1)
        if (location != null) {
            locations.add(location)
        }
    }
    return locations
}


/**
 * 将轨迹点集转化为
 * String
 */
fun getLatLngPathLineString(list: List<LatLng>): String {
    if (list == null || list.isEmpty()) {
        return ""
    }
    val pathline = StringBuffer()
    for (i in list.indices) {
        val location = list[i]
        val locString: String = amapLocationToString(location)
        pathline.append(locString).append(";")
    }
    var pathLineString = pathline.toString()
    pathLineString = pathLineString.substring(
        0,
        pathLineString.length - 1
    )
    return pathLineString
}


/**
 * 将秒转化为 HH:mm:ss的形式
 */
fun formatseconds(seconds: Long): String? {
    val hh = if (seconds / 3600 > 9) (seconds / 3600).toString() + "" else "0${seconds / 3600}"
    val mm =
        if (seconds % 3600 / 60 > 9) (seconds % 3600 / 60).toString() + "" else "0${seconds % 3600 / 60}"
    val ss =
        if (seconds % 3600 % 60 > 9) (seconds % 3600 % 60).toString() + "" else "0${seconds % 3600 % 60}"
    return "$hh:$mm:$ss"
}